<template>
  <div>
    <el-form ref="form" :model="FEditCompany" label-width="80px">
      <el-form-item label="组织名称">
        <el-input v-model="FEditCompany.FCompanyName"></el-input>
      </el-form-item>
      <el-form-item label="组织代码">
        <el-input v-model="FEditCompany.FCompanyCode"></el-input>
      </el-form-item>
      <el-form-item label="上级组织">
        <el-cascader
          v-model="FEditCompany.tempPCompanyID"
          :options="FElmDataTree"
          :props="{ checkStrictly: true }"
          filterable
          clearable
          placeholder="单击选择"
          size="medium"
        ></el-cascader>
      </el-form-item>
      <el-form-item label="组织类型">
        <el-cascader
          v-model="FEditCompany.tempClassID"
          :options="FClassElmDataTree"
          :props="{ checkStrictly: true }"
          filterable
          clearable
          placeholder="单击选择"
          size="medium"
        ></el-cascader>
      </el-form-item>
      <el-form-item>
        <el-row type="flex" justify="end">
          <el-switch
            v-model="FEditCompany.FStatusCode"
            active-text="启用"
            inactive-text="禁用"
            active-value="启用"
            inactive-value="禁用"
          >
          </el-switch>
        </el-row>
      </el-form-item>
    </el-form>
    <el-row justify="end">
      <el-button type="primary" @click="onSave" :disabled="!zIsOpenData"
        >保存</el-button
      >
      <el-button type="warning" @click="onCancel">取消</el-button>
    </el-row>
  </div>
</template>

<script  lang="ts">
import {
  TBaseCompany,
  TBaseCompanyClass,
} from "@/baselib/right/BaseRightClass";
import { Options, prop, Vue } from "vue-class-component";
import { Prop } from "vue-property-decorator";
import TFormBase from "@/views/BaseForm";
import elmHelper, { IElmTreeData } from "../../model/elmHelper";
//配合着用
@Options({
  name: "TFormCompanyEdit",
  components: {},
  computed: {},
  data() {},
})
export default class TFormCompanyEdit extends TFormBase {
  private FEditCompany: TBaseCompany = new TBaseCompany();
  private FCompanyList: TBaseCompany[] = [];
  private FElmDataTree: IElmTreeData[] = [];
  //
  private FClassList: TBaseCompanyClass[] = [];
  private FClassElmDataTree: IElmTreeData[] = [];
  public async created() {
    this.FEditCompany = this.zContextParams.IndexCompany;
    this.FEditCompany.tempPCompanyID = this.FEditCompany.FPCompanyID;
    this.FEditCompany.tempClassID = this.FEditCompany.FCompanyClassID;
    this.FCompanyList = this.zContextParams.CompanyList;
    //转树形数据
    this.FElmDataTree = elmHelper.dataTreeToElmTree(
      this.FCompanyList,
      "FCompanyID",
      "children",
      "FCompanyName",
      "FCompanyID"
    );
    if (!this.$myStrHelper.stringIsEmpty(this.FEditCompany.FCompanyID)) {
      //this.FEditCaption = "结构类型编辑";
    }
    await this.openDataClassList();
    this.openData();
  }
  //打开数据
  public async openData() {
    if (!this.$myStrHelper.stringIsEmpty(this.FEditCompany.FCompanyID)) {
      //打开数据
      this.zIsOpenData = false;
      let lResult = await this.$myBaseRightAPI.getCompany(
        this.FEditCompany.FCompanyID
      );
      if (!lResult.resultSuccess) {
        this.$myMsgHelp.msgDialog(lResult.resultMsg);
        return;
      }
      this.FEditCompany = lResult.resultData;
      if (this.$myStrHelper.stringIsEmptyYW(this.FEditCompany.FCompanyID)) {
        this.$myMsgHelp.msgDialog(
          "返回的数据结构有问题:" + JSON.stringify(lResult.resultData)
        );
        return;
      }
      this.FEditCompany.tempPCompanyID = this.FEditCompany.FPCompanyID;
      this.FEditCompany.tempClassID = this.FEditCompany.FCompanyClassID;
      this.zIsOpenData = true;
    } else {
      this.zIsOpenData = true;
    }
  }
  public async openDataClassList() {
    let lResult = await this.$myBaseRightAPI.getCompanyClassList();
    if (!lResult.resultSuccess) {
      this.$myMsgHelp.msgDialog(lResult.resultMsg);
      return;
    }
    this.FClassList = lResult.resultData;
    if (this.FClassList.length > 0) {
      if (this.$myStrHelper.stringIsEmpty(this.FClassList[0].FClassID)) {
        this.$myMsgHelp.msgDialog("返回的数据格式和前端的类定义不一至,请检查");
        return;
      }
    }
    //
    this.FClassElmDataTree = elmHelper.dataTreeToElmTree(
      this.FClassList,
      "FClassID",
      "children",
      "FClassName",
      "FClassID"
    );
  }
  //
  private async onSave() {
    //当清空是elm返回的是null,多级返回的是数组
    if (this.FEditCompany.tempPCompanyID == null) {
      this.FEditCompany.FPCompanyID = "";
    } else if (this.FEditCompany.tempPCompanyID instanceof Array) {
      let temp =
        this.FEditCompany.tempPCompanyID[
          this.FEditCompany.tempPCompanyID.length - 1
        ];
      if (this.FEditCompany.FCompanyID == temp) {
        this.FEditCompany.tempPCompanyID = this.FEditCompany.FPCompanyID;
        this.FEditCompany.tempClassID = this.FEditCompany.FCompanyClassID;
      } else {
        this.FEditCompany.FPCompanyID = temp;
      }
    }
    //
    if (this.FEditCompany.tempClassID == null) {
      this.FEditCompany.FCompanyClassID = "";
    } else if (this.FEditCompany.tempClassID instanceof Array) {
      let temp =
        this.FEditCompany.tempClassID[this.FEditCompany.tempClassID.length - 1];
      this.FEditCompany.FCompanyClassID = temp;
    }
    //保存数据
    let lResult = await this.$myBaseRightAPI.saveCompany(this.FEditCompany);
    if (!lResult.resultSuccess) {
      this.$myMsgHelp.msgDialog(lResult.resultMsg);
      return;
    }
    this.$myMsgHelp.msgHintAutoClose("保存数据成功",this.$myMsgHelp.msgSuccess);
    if (this.zContextOnCall != null && this.zContextOnCall != undefined) {
      this.zContextOnCall("save", this.FEditCompany);
    }
  }
  private onCancel() {
    if (this.zContextOnCall != null && this.zContextOnCall != undefined) {
      this.zContextOnCall("close", this.FEditCompany);
    }
  }
}
</script>